<?php

class Message
{
	protected $_id;
	protected $_contest_id;
	protected $_messageDate;
	protected $_responseMessage_id;
	protected $_messageFrom;
	protected $_mailFrom;
	protected $_messageTo;
	protected $_mailTo;
	protected $_subject;
	protected $_text;
	protected $_sendMail;
	protected $_inInbox;
	protected $_category_id;
	
	public function __construct(array $donnees)
	{
		$this->hydrate($donnees);
	}
		
	public function hydrate(array $donnees)
	{
		foreach($donnees as $key => $value)
		{
			$method = 'set'. ucfirst($key);
			if(method_exists($this, $method))
			{
				$this->$method($value);
			}
		}
	}
	
	public function id(){ return $this->_id; }
	public function contest_id(){ return $this->_contest_id; }
	public function messageDate(){ return $this->_messageDate; }
	public function responseMessage_id(){ return $this->_responseMessage_id; }
	public function messageFrom(){ return $this->_messageFrom; }
	public function mailFrom(){ return $this->_mailFrom; }
	public function messageTo(){ return $this->_messageTo; }
	public function mailTo(){ return $this->_mailTo; }
	public function subject(){ return $this->_subject; }
	public function text(){ return $this->_text; }
	public function sendMail(){ return $this->_sendMail; }
	public function inInbox(){ return $this->_inInbox; }
	public function category_id(){ return $this->_category_id; }
	
	public function setId($id)
	{
		$id = (int) $id;
		if(is_int($id))
		{
			$this->_id = $id;
		}
	}
	public function setContest_id($contest_id)
	{
		$contest_id = (int) $contest_id;
		if(is_int($contest_id))
		{
			$this->_contest_id = $contest_id;
		}
	}
	public function setMessageDate($messageDate)
	{
		$this->_messageDate = $messageDate;
	}
	public function setResponseMessage_id($message_id)
	{
		$message_id = (int) $message_id;
		if(is_int($message_id))
		{
			$this->_responseMessage_id = $message_id;
		}
	}
	public function setMessageFrom($messageFrom)
	{
		$messageFrom = (int) $messageFrom;
		if(is_int($messageFrom))
		{
			$this->_messageFrom = $messageFrom;
		}
	}
	public function setMailFrom($mailFrom)
	{
		if(is_string($mailFrom))
		{
			$this->_mailFrom = $mailFrom;
		}
	}
	public function setMessageTo($messageTo)
	{
		$this->_messageTo = $messageTo;
	}
	public function setMailTo($mailTo)
	{
		if(is_string($mailTo))
		{
			$this->_mailTo = $mailTo;
		}
	}
	public function setSubject($subject)
	{
		$subject = (string) $subject;
		if(is_string($subject))
		{
			$this->_subject = $subject;
		}
	}
	public function setText($text)
	{
		$text = (string) $text;
		if(is_string($text))
		{
			$this->_text = $text;
		}
	}
	public function setSendMail($sendMail)
	{
		$sendMail = (bool) $sendMail;
		if(is_bool($sendMail))
		{
			$this->_sendMail = $sendMail;
		}
	}
	public function setCategory_id($category_id)
	{
		$category_id = (int) $category_id;
		if(is_int($category_id))
		{
			$this->_category_id = $category_id;
		}
	}
	public function setInInbox($inInbox)
	{
		$inInbox = (bool) $inInbox;
		if(is_bool($inInbox))
		{
			$this->_inInbox = $inInbox;
		}
	}
	
	public function getRecipients()
	{
		if($this->messageTo() != "" && $this->messageTo() != "0")
		{
			$messageTo=explode(';', $this->messageTo());
			$recipients = array();
			foreach($messageTo as $to)
			{
				$to = (int) substr($to, 1);
				$recipients[] = $to;
			}
			return $recipients;
		}
		else return null;
	}
}